<?php
/**
 * Copyright (c) 2013 pipapai.com
 */
JLoader::register('PResumesModelResume', OC::$SERVERROOT."/apps/resumes/lib/resume.php");
JLoader::register('PResumesModelUpload', OC::$SERVERROOT."/apps/resumes/lib/upload.php");
JLoader::register('PResumesModelTag', OC::$SERVERROOT."/apps/resumes/lib/tag.php");
JLoader::register('PResumesHelper', OC::$SERVERROOT."/apps/resumes/lib/helper.php");

class PResumeController {

   // add to list, in temporary save status and will be removed if space is approaching 0 
   public static function add() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume(); 
      if(!$model->add()) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true; 
   }

   // remove the existing resume 
   public static function trash() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume(); 
      if(!$model->trash()) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true;
   }

   // remove the existing resume 
   public static function restore() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume();
      if(!$model->trash(false)) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true;
   }

  // perm remove the existing resume from a company 
   public static function permdelete() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume();
      if(!$model->permdelete()) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true;
   }

   // update the existing resume 
   public static function update() {

   }

   // save to cloud, cannot be removed unless operated by user 
   public static function save() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume();
      if(!$model->save()) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true; 
   }

   // remove save status.  
   public static function unsave() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      $model = new PResumesModelResume();
      if(!$model->save(false)) {
         OCP\JSON::error(array('error' => $model->getError()));
         return false;
      }

      return true; 
   }

   public static function view() {
      if (!PUtil::checkUserLogin(true,null)) {
         return;
      }

      // fint out from dir 
      $app = PFactory::getApplication();
      
      $companyid = 0;
      if(!OC_User::isLoggedIn()) {
         $companyid = $app->getUserState('resumeshare_companyid', 0);
         if(!$companyid) {
            OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
            return;
         }
      } else {
         if (PFactory::isJobseekerUser()) {
            OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
            return;
         } else {
            $user = PFactory::getMasterUser();
            $companyid = $user->id;
         }
      }

      $fromdir = 'all';
      $token = JRequest::getString('t', '');
      $searchtokens = $app->getUserState('searchtokens', null);
      $searchtoken = (!empty($token) && !empty($searchtokens[$token])) ? $searchtokens[$token] : null;
      if(!empty($searchtoken)) {
         $searchtokens[$token]['count']++;
         $app->setUserState('searchtokens', $searchtokens);
         $fromdir = JArrayHelper::getValue($searchtoken, 'dir', 'all');
      }

      $model = new PResumesModelResume();
      $permissions = OCP\PERMISSION_READ;
      if(PCompanyHelper::isJobManager() && $fromdir != 'shared') {
         $resumes = self::getResumeAsFiles();
      }
      else {
         $resumes = self::getSharedResumeAsFiles();
      }

      if(empty($resumes)) {
         $tmpl = new OC_Template('', '404', 'user');
         OCP\App::setActiveNavigationEntry('resumes_index');
         $tmpl->assign('error', '简历未找到');
         $tmpl->printPage();
         return;
      }

      // only allow view one resume at a time      
      $resume = $resumes[0]; 
      $dir = 'all';
      $resumeEdit = false;
      if(strpos($resume->source, 'upload') !==FALSE && PResumesHelper::checkResumeUploadPermission($resume)){
         $resumeEdit=true;
      }
      $interviewpermissions = false;
      if(PCompanyHelper::checkJobManagerPermissions($resume->jobid)) {
         // as admin user, it has full permissions 
         $permissions |= OCP\PERMISSION_CREATE;
         $permissions |= OCP\PERMISSION_UPDATE;
         $permissions |= OCP\PERMISSION_DELETE;
         $permissions |= OCP\PERMISSION_SHARE;
         $interviewpermissions = true;
      }
      elseif(isset($resume->permissions)) {
         $permissions = $resume->permissions; 
      }

      if($resume->is_saved) 
         $dir = 'saved';
      else 
         $dir = 'unsaved'; 

      if($resume->is_trashed) 
         $dir = 'trashed';

      /* regardless of fromdir, if the resume is trashed, it's from /Trashed virtual folder */
      if($resume->is_trashed)
         $fromdir = 'trashed';

      // get next available resume 
      list($prevresume, $nextresume) = $model->getNextResume($resume->id);

      $interviewevent = $model->getJobseekerInterviewEvent($resume->id);

      $resumeStatuses = PResumesHelper::getResumeStatuses();

      // get persontest info

      $resume->pResult = '没有结果';
      $persontestResultInfo = $model->getPersontestResultInfo($companyid, $resume->id, true);
      if (!empty($persontestResultInfo->shortresult)) {
         $resume->pResult = $persontestResultInfo->shortresult;
      }

      // convert date_added to unix timestamp 
      $resume->date_added = PFactory::getDate($resume->date_added)->toUnix(); 

      // get resume tags
      $tagmodel = new PResumesModelTag();
      $resumeTags = $tagmodel->getResumeTags($resume->id, $companyid);

      // get resume comments
      $resumeCommentsInfo = $model->getResumeComments(PFactory::getMasterUser()->id, $resume->id, 1);
      CacheHelper::triggerRemove($resume->id, array("*PREFIX*hr_jobseeker_comments-".$resume->id));
      $resumeComments = $resumeCommentsInfo["list"];
      $countResumeComments = $resumeCommentsInfo["total"];

      // make breadcrumb und filelist markup
      $breadcrumbNav = new OCP\Template('resumes', 'part.resume.breadcrumb', '');
      $breadcrumbNav->assign('breadcrumb', $resume);
      $breadcrumbNav->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $breadcrumbNav->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));

      $part_resume_top = new OCP\Template('resumes', 'part.resume.top', '');
      $part_resume_top->assign('fromdir', $fromdir);
      $part_resume_top->assign('breadcrumb', $breadcrumbNav->fetchPage());

      $tmpl = new OCP\Template('resumes', 'resume', 'user');      
      $tmpl->assign('page_title', '云简历');
      $tmpl->assign('permissions', $permissions);
      $tmpl->assign('interviewpermissions',$interviewpermissions);
      $tmpl->assign('dir', $dir);
      $tmpl->assign('fromdir', $fromdir);
      $tmpl->assign('interviewevent',$interviewevent);
      $tmpl->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true)));
      $tmpl->assign('part_resume_top', $part_resume_top->fetchPage());

      $tmpl->assign('resume', $resume);
      $tmpl->assign('resumeTags', $resumeTags);
      $tmpl->assign('resumeComments', $resumeComments);
      $tmpl->assign('countResumeComments', $countResumeComments);
      $tmpl->assign('resumeStatuses', $resumeStatuses);
      $tmpl->assign('prevresume', $prevresume);
      $tmpl->assign('nextresume', $nextresume);
      $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $tmpl->assign('downloadURL', OCP\Util::linkToRoute('resume_download', array('resume' => '/')));
      $tmpl->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $tmpl->assign('disableSharing', false);
      $tmpl->assign('resumeEdit', $resumeEdit);
      $tmpl->assign('searchtoken', $token);
      $tmpl->printPage();

   }
   
   public static function getResumeAsFiles($companyid=0) {
      $resumeid = JRequest::getInt('id', 0); 
      $resumeids = JRequest::getString("ids", "");
      if(empty($resumeid) && empty($resumeids))
         return true; 

      if(!empty($resumeids))
         $resumeids = explode(",", $resumeids); 
      else {
         $resumeids = array(); 
         array_push($resumeids, $resumeid);
      }

      if(!$companyid) {
         $user = PFactory::getMasterUser(); 
         $companyid = $user->id; 
         if(!$companyid)
            return false; 
      }

      $model = new PResumesModelResume(); 
      $resumes = $model->getResumeAsFiles($companyid, $resumeids); 

      $extdir = getUserExtDir($companyid);

      foreach($resumes as &$resume) {
         $resume_storagepath = PStorage::getResumeStoragePath($resume->hashkey);
         if(!$resume_storagepath) {
            // this should not happen. if the hashkey is null, we might reach here, but that means the resume is bad 
            $resume_storagepath = "/not-exist"; 
         }

         //dummy link; real file is based on source
         $resume->storage_path = $resume_storagepath; 
         $resume->resumefile = $resume_storagepath . "/" . $resume->version . "/resume.html"; 
      }
      return $resumes; 
   }

   public static function getSharedResumeAsFiles($companyid=0) {
      $resumeid = JRequest::getInt('id', 0);
      $resumeids = JRequest::getString("ids", "");
      if(empty($resumeid) && empty($resumeids))
         return true;

      if(!empty($resumeids))
         $resumeids = explode(",", $resumeids);
      else {
         $resumeids = array();
         array_push($resumeids, $resumeid);
      }

      if(!$companyid) {
         $user = PFactory::getMasterUser();
         $companyid = $user->id;
         if(!$companyid)
            return false;
      }

      $model = new PResumesModelResume();
      $resumes = $model->getSharedResumeAsFiles($companyid, $resumeids);

      foreach($resumes as $resume) {
         //dummy link; real file is based on source 
         $resume->resumefile = "/".$companyid."/resumes/" . $resume->id . "/resume.html";
      }
      return $resumes;
   }

   public static function setResumeNewStatus($resumeid=0) {
      if(!$resumeid)
         return; 

      $model = new PResumesModelResume(); 
      $model->setResumeNewStatus($resumeid);
   }
   
   public static function setResumeStatus() {
      $jobid = JRequest::getInt('jobid', 0);
      $resumeid = JRequest::getInt('resumeid', 0);
      $status_id = JRequest::getInt('status_id', 0);
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid) || !$resumeid || !$status_id) {
         OCP\JSON::error(array('error' => '没有权限'));
         return false;
      }
   
      PResumesHelper::setResumeStatus($resumeid, $status_id);
      
      return true;
   }
   
   // save tags for resume
   public static function saveResumeTags() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('jobid', 0);
      $resumeid = JRequest::getInt('resumeid', 0);
      $tags = JRequest::getVar("tagit", array(), 'POST', 'array');
      
      if (!empty($tags)) {
         $data['jobid'] = $jobid;
         $data['resumeid'] = $resumeid;
         $data['owner'] = $user->id;
         $data['tags'] = implode(",", $tags['tags']);
         
         $model = new PResumesModelTag();
         if(!$model->saveResumeTags($data)) {
            OCP\JSON::error(array('error' => '标签保存失败'));
            return false;
         }
      }
   
      return true;
   }
   
   // add tags for resume
   public static function addResumeTags() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
   
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('jobid', 0);
      $resumeid = JRequest::getInt('resumeid', 0);
      $tags = JRequest::getVar("tagit", array(), 'POST', 'array');
   
      if (!empty($tags)) {
         $data['jobid'] = $jobid;
         $data['resumeid'] = $resumeid;
         $data['owner'] = $user->id;
         $data['tags'] = implode(",", $tags['tags']);
          
         $model = new PResumesModelTag();
         if(!$model->addResumeTags($data)) {
            $errorMessage = $model->getError() ? $model->getError() : '标签添加失败';
            OCP\JSON::error(array('error' => $errorMessage));
            return false;
         }
      }
       
      return true;
   }

   public static function reviewPaper() {
      $user = PFactory::getMasterUser();
      $companyid = $user->id;

      $jobseeker_id = JRequest::getInt('id', 0);

      $adminuser = false;
      if(!OC_User::isLoggedIn()) {
         $app = PFactory::getApplication();
         $companyid = $app->getUserState('resumeshare_companyid', 0);
         if(!$companyid) {
            OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
            return;
         }
         $adminuser = true;
      } else {
         if (PFactory::isJobseekerUser()) {
            OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
            return;
         }
      }

      $interview_progess = PInterviewHelper::getInterviewProgress($jobseeker_id,$companyid);
      if (!$interview_progess || !isset($interview_progess['onlinetest'])) {
         $tmpl = new OC_Template('', '404', 'user');
         $tmpl->assign('error', '答卷未找到');
         $tmpl->printPage();
         return;
      }

      $model = new PResumesModelResume();

      $basic_info = $model->getPaperBasicInfo($jobseeker_id);
      $questions = $model->getUserPaperResultFreeinput($jobseeker_id, $basic_info->sheetid);
      $correct_rate = $model->getCorrectRate($jobseeker_id, $basic_info->sheetid);
      $testRank = $model->getTestRank($basic_info->jobid, $basic_info->online_interview_testscore);
      $during_sec = PFactory::getDate($basic_info->finished_date)->toUnix() - PFactory::getDate($basic_info->accept_date)->toUnix();
      $during_min = ceil($during_sec/60);

      if (empty($basic_info) || empty($correct_rate)) {
         $tmpl = new OC_Template('', '404', 'user');
         $tmpl->assign('error', '答卷未找到');
         $tmpl->printPage();
         return;
      }
      
      if (PCompanyHelper::isJobManager() && (PCompanyHelper::checkJobManagerPermissions($basic_info->jobid) || PResumesHelper::isSharedResume(OC_User::getUser(), $jobseeker_id))) {
         $adminuser = true;
      }

      // make breadcrumb
      $breadcrumbNav = new OCP\Template('resumes', 'part.resume.breadcrumb', '');
      $breadcrumbNav->assign('breadcrumb', $basic_info);
      $breadcrumbNav->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $breadcrumbNav->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $breadcrumbNav->assign('resumelink', true);

      $tmpl = new OCP\Template('resumes', 'reviewpaper', 'user');
      $tmpl->assign('breadcrumb', $breadcrumbNav->fetchPage());
      $tmpl->assign('page_title', '试卷批阅');
      $tmpl->assign('basic_info', $basic_info);
      $tmpl->assign('during_min', $during_min);
      $tmpl->assign('testRank', $testRank);
      $tmpl->assign('questions', $questions);
      $tmpl->assign('correct_rate', $correct_rate);
      $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $tmpl->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $tmpl->assign('adminuser', $adminuser);
      $tmpl->printPage();
   }

   public static function showSingleAnswer() {
      $jobseeker_id = JRequest::getInt('jid', 0);
      $type = JRequest::getString('vtype', '');
      $sheetid = JRequest::getString('sid', '');
      $redirect_url = OC_Helper::linkToRoute('resume_reviewpaper', array('id' => $jobseeker_id));
      
      $companyid = 0;
      if(!OC_User::isLoggedIn()) {
         $app = PFactory::getApplication();
         $companyid = $app->getUserState('resumeshare_companyid', 0);
      } else {
         if (!PFactory::isJobseekerUser()) {
            $companyid = PFactory::getMasterUser()->id;
         }
      }
      
      if(!$companyid) {
         if (!OC_User::isLoggedIn()) {
            echo PUtil::checkUserLogin(true, $redirect_url);
         } else {
            echo '没有权限';
         }
         return;
      }

      $model = new PResumesModelResume();

      $questions = $model->getUserPaperResultObjective($jobseeker_id,$type,$sheetid);

      if(empty($questions))
         return;

      $tmpl = new OCP\Template('resumes', 'part.reviewpaper', '');
      $tmpl->assign('questions', $questions);
      $tmpl->printPage();
   }

   public static function postScore() {
      if (!PUtil::checkUserLogin(true,null)) {
         return;
      }

      $model = new PResumesModelResume();
      $post = JRequest::get("post");
      $jobseeker_id = $post['jobseeker_id'];

      if (!$model->postScore($post)) {
         jexit(jsonError($model->getError(), null, false));
      }

      jexit(jsonSuccess('success', OC_Helper::linkToRoute("resume_reviewpaper")."?id=".$jobseeker_id, null, false));
   }
   
   // add tags for resume
   public static function inviteJobseeker() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      
      $user = PFactory::getMasterUser();
      
      $model = new PResumesModelResume();
      $data = $model->checkInviteOnLineInterviewRequirements();
      if($data === false) {
         $errorMessage = $model->getError() ? $model->getError() : '邀请失败';
         OCP\JSON::error(array('error' => $errorMessage));
         return;
      }

      $data = $model->inviteJobseeker($user->id);
      if($data === false) {
         $errorMessage = $model->getError() ? $model->getError() : '邀请失败';
         OCP\JSON::error(array('error' => $errorMessage));
      } else {
         if ($data === true) {
            OCP\JSON::success();
         } else {
            OCP\JSON::success($data);
         }
      }
      exit();
   }
   
   public static function showAddForm(){
      if(!PUtil::checkUserLogin(true))
         return;
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      $app=PFactory::getApplication();
      $user = PFactory::getMasterUser();      
      $fromdir = 'all';
      $jobseeker_id=JRequest::getInt('jobseeker_id', 0);
      $type = $jobseeker_id ? 'edit' : 'add';
      $title = $type == 'add' ? '创建简历' : '编辑简历';

      $model = new PResumesModelResume();
      $jobseekerinfo=$model->getResumeJobDetail($jobseeker_id, $user->id);
      $errorMsg='';
      if($type=='add'){
         if(!PCompanyHelper::isJobAdmin()) {
            $errorMsg='您无权访问该资源';
         }
      }else{         
         if(empty($jobseekerinfo) || strpos($jobseekerinfo->source,'upload')===FALSE){
            $errorMsg='该简历可能不存在或者不能编辑';
         }elseif(!PResumesHelper::checkResumeUploadPermission($jobseekerinfo)){
            $errorMsg='您无权访问该资源';
         }
      }
      if(!empty($errorMsg)){
         $tmpl = new OC_Template('', '404', 'user');
         OCP\App::setActiveNavigationEntry('resumes_index');
         $tmpl->assign('error', $errorMsg);
         $tmpl->printPage();
         return;
      } 
      $part_resume_top = new OCP\Template('resumes', 'part.resume.top', '');
      $part_resume_top->assign('fromdir', $fromdir);
      $part_resume_top->assign('type',$type);     
      $part_resume_top->assign('showbatchupload',true);     
      $part_resume_top->assign('breadcrumb', '<div style="line-height:22px;margin-top:8px" class="crumb">当前位置：</div><div class="crumb last"><a href="javascript:;" style="cursor:default;"> '.($type=="add" ? '创建简历':'编辑简历')."</a></div>");
      $part_resume_add = new OCP\Template('resumes', 'part.addresume', '');
      $part_resume_add->assign('jobseekerinfo', $jobseekerinfo);
      $part_resume_add->assign('type',$type);
      
      $tmpl = new OCP\Template('resumes', 'addresume', 'user');  
      $tmpl->assign('fromdir', $fromdir);
      $tmpl->assign('page_title', $title);
      $tmpl->assign('part_resume_top', $part_resume_top->fetchPage());
      $tmpl->assign('part_resume_add', $part_resume_add->fetchPage());
      $tmpl->printPage();
   }
   
   public static function saveAddResume(){
      if(!PUtil::checkUserLogin(true))
         return;
      $model = new PResumesModelUpload();
      $jobseeker_id=$model->saveAddResume();
      if(!$jobseeker_id) {
         jexit(jsonError($model->getError(), null, false));
      }else{
         $action=OC_Helper::linkToRoute('resume_view',array('id'=>$jobseeker_id,'name'=>JRequest::getString('name','')));       
         jexit(jsonSuccess(JText::_('Success to save'),$action));
      }
   }
   
   public static function uploadResumeAttachment(){
      if(!PUtil::checkUserLogin(true))
         return;
      OC_JSON::setContentTypeHeader('text/plain');
      $model = new PResumesModelUpload();
      jexit($model->uploadResumeAttachment());
   }

   public static function batchUpload() {
      if(!PUtil::checkUserLogin(true))
         return;

      OC_JSON::setContentTypeHeader('text/plain');
      $model = new PResumesModelUpload();
      jexit($model->batchUpload()); 
   }

   // add comments for resume
   public static function addResumeComments() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '没有权限'));return;
      }

      $userid = PFactory::getMasterUser()->id;
      $jobseeker_id = JRequest::getInt('jobseeker_id', 0);
      $commenttext = JRequest::getString("commenttext", "");
      $type = JRequest::getInt("type", 0);
      $time = PFactory::getDate();
      $jobid = JRequest::getInt("jobid", 0);
      $limit = JRequest::getInt("limit", 0);

      if (!$jobseeker_id) {
         OCP\JSON::error(array('error' => '添加评语失败'));return;
      }
      if (!$commenttext) {
         OCP\JSON::error(array('error' => '请填写评论内容'));return;
      }
      if (!$type) {
         OCP\JSON::error(array('error' => '请选择评语类型'));return;
      }
      if (compact_strlen($commenttext) > 500 || compact_strlen($commenttext) <= 0) {
         OCP\JSON::error(array('error' => '评语字符数不符合要求'));return;
      }
      if($jobid == 0) {
         OCP\JSON::error(array('error' => '添加评语失败'));return;
      }
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         OCP\JSON::error(array('error' => '没有权限'));return;
      }

      $model = new PResumesModelResume();
      if($insert_id = $model->addResumeComments($jobseeker_id, $commenttext, $userid, $type)) {
         $rows = $model->getResumeComments(PFactory::getMasterUser()->id, $jobseeker_id, $limit);
         OCP\JSON::success(array('list' => $rows["list"], 'total' => $rows["total"], 'left' => $rows["left"], "ismaster"=>PFactory::isMasterUser()));
         return;
      } else {
         OCP\JSON::error(array('error' => '添加评语失败'));return;
      }
   }

   public static function getResumeComments() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::success(array('list' => array(), 'count' => 0));return;
      }

      $jobseeker_id = JRequest::getInt('jobseeker_id', 0);
      $limit = JRequest::getInt("limit", 0);

      $model = new PResumesModelResume();
      $rows = $model->getResumeComments(PFactory::getMasterUser()->id, $jobseeker_id, $limit);
      if(empty($rows)) {
         OCP\JSON::error(array('error' => '获取评语失败'));return;
      } else {
         OCP\JSON::success(array('list' => $rows["list"], 'total' => $rows["total"], 'left' => $rows["left"], "ismaster"=>PFactory::isMasterUser()));
         return;
      }
   }

   public static function deleteResumeComments() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));return;
      }
      $jobseeker_id = JRequest::getInt('jobseeker_id', 0);
      $comment_id = JRequest::getInt("comment_id", 0);
      $limit = JRequest::getInt("limit", 0);

      if(!$jobseeker_id) {
         OCP\JSON::error(array('error' => '删除评语失败'));return;
      }
      if(!$comment_id) {
         OCP\JSON::error(array('error' => '删除评语失败'));return;
      }
      $model = new PResumesModelResume();
      if($model->deleteResumeComments($comment_id, $jobseeker_id, PFactory::getMasterUser()->id)) {
         $rows = $model->getResumeComments(PFactory::getMasterUser()->id, $jobseeker_id, $limit);
         OCP\JSON::success(array('list' => $rows["list"], 'total' => $rows["total"], 'left' => $rows["left"], "ismaster"=>PFactory::isMasterUser()));
         return;
      } else {
         OCP\JSON::error(array('error' => '删除评语失败'));return;
      }
   }
}
